package com.software.car.controller;

import com.software.car.common.entity_change.UserChangeMapper;
import com.software.car.common.enums.ResultEnum;
import com.software.car.common.enums.RoleEnum;
import com.software.car.common.utils.CommonPage;
import com.software.car.common.utils.CommonResult;
import com.software.car.common.utils.JwtUtil;
import com.software.car.entity.dto.UserAdministerUpdateDto;
import com.software.car.entity.dto.UserInsertDto;
import com.software.car.entity.dto.UserPersonalUpdateDto;
import com.software.car.entity.vo.UserDetailVo;
import com.software.car.api.UserService;
import com.software.car.entity.po.UserPo;
import com.software.car.service.FeaturesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @Author 魔法はまだ解けない、林涛
 * @Date 2021/1/21
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     *
     * 超级管理员、管理员
     * 获取所有用户信息，包括密码
     * 默认获取正常状态用户
     *
     * @param page     页号
     * @param pageSize 页大小
     * @param state    用户状态，null为全部用户
     * @param query    查询参数
     * @param role    用户角色
     * @return
     */
    @GetMapping("/users/page")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER')")
    public CommonResult getUsersByPage(@RequestParam(value = "page", required = false) Integer page,
                                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                       @RequestParam(value = "state", required = false) Integer state,
                                       @RequestParam(value = "query", required = false) String query,
                                       @RequestParam(value = "role", required = false) Integer role) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        CommonPage usersByPage = userService.getUsersByPage(page, pageSize, state,query,role);
        return CommonResult.success(usersByPage);
    }

    /**
     * 用户
     * 获取个人信息
     *
     * @param userPo 个人信息
     * @return
     */
    @GetMapping("/personal/message")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_USER')")
    public CommonResult getPersonalMessage(@RequestAttribute("user") UserPo userPo) {
        System.out.println(userPo);
        UserDetailVo userDetailVo = UserChangeMapper.INSTANCE.UserPoToUserPersonalVo(userPo);
        System.out.println(userDetailVo);
        return CommonResult.success(userDetailVo);
    }

    /**
     * 管理员
     * 添加新用户
     *
     * @param userInsertDto
     * @return
     */
    @PostMapping("/administer/user")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER')")
    public CommonResult addUser(@RequestBody UserInsertDto userInsertDto) {
        //检验账号是否存在
        UserDetailVo userByAccount = userService.getUserByAccount(userInsertDto.getAccount());
        if (userByAccount != null) {
            return CommonResult.fail(ResultEnum.ACCOUNT_HAS_EXIT);
        }
        int i = userService.insertUser(userInsertDto);
        if (i == 0) {
            return CommonResult.fail(ResultEnum.INSERT_USER_FAIL);
        }
        return CommonResult.success();
    }

    /**
     * 个人
     * 用户信息修改
     *
     * @param userInsertDto 前端给的个人数据体
     * @return
     */
    @PutMapping("/personal/message")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_USER')")
    public CommonResult userUpdateUser(@RequestBody UserPersonalUpdateDto userInsertDto,
                                       @RequestAttribute("user") UserPo userPo) {
        UserDetailVo userDetailVo = userService.userUpdateUser(userInsertDto, userPo);
        return CommonResult.success(userDetailVo);
    }


    /**
     * 管理员
     * 修改用户信息
     *
     * @param userInsertDto 前端给的修改的用户数据体
     * @return
     */
    @PutMapping("/administer/message")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_USER')")
    public CommonResult administerUpdateUser(@Validated @RequestBody UserAdministerUpdateDto userInsertDto) {
        UserDetailVo userDetailVo = userService.administerUpdateUser(userInsertDto);
        return CommonResult.success(userDetailVo);
    }

    /**
     * 个人用户密码修改
     *
     * @param oldPassword
     * @param newPassword
     * @param userPo
     * @return
     */
    @PutMapping("/personal/password")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_USER')")
    public CommonResult userUpdatePassword(@RequestParam(value = "oldPassword") String oldPassword,
                                           @RequestParam(value = "newPassword") String newPassword,
                                           @RequestAttribute("user") UserPo userPo) {
        if (userPo.getPassword().equals(oldPassword)) {

            UserAdministerUpdateDto userPo1 = new UserAdministerUpdateDto();
            userPo1.setId(userPo.getId());

            userPo1.setPassword(newPassword);
            userService.administerUpdateUser(userPo1);

            return CommonResult.success();
        } else {
            return CommonResult.fail(ResultEnum.UPDATE_ERROR);
        }
    }

    /**
     * 管理员用户注销功能
     *
     * @param id
     * @return
     */
    @DeleteMapping("/administer/users/delete")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER')")
    public CommonResult deleteUser(@RequestParam String id,
                                   @RequestAttribute("user") UserPo me) {

        UserAdministerUpdateDto userPo1 = new UserAdministerUpdateDto();
        userPo1.setId(id);

        userPo1.setState(2);
        userService.administerUpdateUser(userPo1);

        return CommonResult.success();
    }

    /**
     * 管理员激活用户
     *
     * @param id
     * @return
     */
    @PutMapping("/administer/users/activation")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER')")
    public CommonResult activationUser(@RequestParam String id,
                                       @RequestAttribute("user") UserPo me) {

        UserAdministerUpdateDto userPo1 = new UserAdministerUpdateDto();
        userPo1.setId(id);
        userPo1.setState(1);
        userService.administerUpdateUser(userPo1);
        return CommonResult.success();
    }



}